package com.myspace.activity;

import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.myspace.sdk.MSLoginActivity;
import com.myspace.sdk.MSRequest;
import com.myspace.sdk.MSSDK;
import com.myspace.sdk.MSSession;
import com.myspace.sdk.MSRequest.MSRequestCallback;
import com.myspace.sdk.MSSession.IMSSessionCallback;

public class MainActivity extends MSBaseActivity {

	private static final String API_KEY = "";
	private static final String API_SECRET = "";
	private static final String API_CALLBACK_URL = "";
	
	private MSSession msSession;
	private Button mLoginButton;
	private Button mLogoutButton;
	private Button mStatusMoodButton;
	private Button mFriendsButton;
	private Button mPhotoButton;
	private Button mVideoButton;
	private Button mActivityStreamButton;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setMainView(R.layout.main);
		
		msSession = MSSession.getSession(API_KEY, API_SECRET, API_CALLBACK_URL, new MSSessionCallbackImpl());
		
		// request permissions for REST APIs
		msSession.requestPermissions("UpdateMoodStatus|AddPhotosAlbums");
		
		mLoginButton = (Button) findViewById(R.id.loginButton);
		mLoginButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				gotoPage(MSLoginActivity.class);
			}
		});

		mLogoutButton = (Button) findViewById(R.id.logoutButton);
		mLogoutButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				logout();
			}
		});

		mStatusMoodButton = (Button) findViewById(R.id.statusMoodButton);
		mStatusMoodButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				gotoPage(MSStatusMoodActivity.class);
			}
		});

		mFriendsButton = (Button) findViewById(R.id.friendsButton);
		mFriendsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				gotoPage(MSFriendsActivity.class);
			}
		});

		mPhotoButton = (Button) findViewById(R.id.photoButton);
		mPhotoButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				gotoPage(MSPhotoActivity.class);
			}
		});

		mVideoButton = (Button) findViewById(R.id.videoButton);
		mVideoButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				gotoPage(MSVideoActivity.class);
			}
		});

		mActivityStreamButton = (Button) findViewById(R.id.activityStreamButton);
		mActivityStreamButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				gotoPage(MSActivityStreamActivity.class);
			}
		});
		
		boolean isSessionActive = msSession.resume(this);
		if(isSessionActive) {
			(new MSSessionCallbackImpl()).sessionDidLogin(msSession);
		} else {
			(new MSSessionCallbackImpl()).sessionDidLogout(msSession);
		}
	}

	private void logout() {
		msSession.end(this);
	}

	private class MSSessionCallbackImpl implements IMSSessionCallback {

		public void sessionDidLogin(MSSession session) {
			mLoginButton.setEnabled(false);
			mLogoutButton.setEnabled(true);
			mStatusMoodButton.setEnabled(true);
			mFriendsButton.setEnabled(true);
			mPhotoButton.setEnabled(true);
			mVideoButton.setEnabled(true);
			mActivityStreamButton.setEnabled(true);
			
			MSSDK.getUserInfo(new GetUserInfo_MSRequestCallback());
		}

		public void sessionDidLogout(MSSession session) {
			mLoginButton.setEnabled(true);
			mLogoutButton.setEnabled(false);
			mStatusMoodButton.setEnabled(false);
			mFriendsButton.setEnabled(false);
			mPhotoButton.setEnabled(false);
			mVideoButton.setEnabled(false);
			mActivityStreamButton.setEnabled(false);
			
			setPageName(R.string.PAGE_TITLE_MAIN);
		}
	}
	
	private class GetUserInfo_MSRequestCallback extends MSRequestCallback {

		public void requestDidFail(MSRequest request, Throwable error) {
			showToast(error.getMessage());
		}

		public void requestDidLoad(MSRequest request, Object result) {
			Map<?,?> data = (Map<?,?>)result;
			result  = data.get("data");
			
			if(result instanceof Map<?,?>) {
				Map<?,?> userObject = (Map<?,?>)result;
				String userName = (String)userObject.get("userName");
				setPageName("Hello " + userName);
			}
		}
	}
}